
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using Syrinx.Gui;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Gui;
using Swaf.Security;

namespace MediaCache.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public class MediaCacheManagementBizProc : BaseBusinessProcess
    {
		public MediaCacheManagementBizProc(ActiveBizProc procState)
            :base(procState)
        {
			
		}

		protected override void setupRuleNames()
		{
			base.m_getBizObjListRule = "getCompanyMediaCacheLibraries";
			base.m_getBizObjDetailsRule = "getImageCacheDetails";
			base.m_updateBizObjRule = "updateCompanyImageCacheLibrary";
		}

		public override Privileges CurrentEditPrivilege
		{
			get { return Privileges.Delete; }
		}

		protected override object[] getBizObjListRuleParameters(object parentId, IBizObj filterSort)
		{
			return new object[] { };
		}

		protected override bool shouldCallGetBizObjListRule(object parentBizObjId)
		{
			return true;
		}
		/*
		/// <summary>
        /// 
        /// </summary>
		public override void initiateDataRetrieval(ParentBizProcInfo parent)
        {
            initiateDataRetrieval("getCompanyMediaCacheLibraries", "");
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ruleName"></param>
        /// <param name="p"></param>
        public void initiateDataRetrieval(string ruleName, params object[] p)
        {
            m_rules = App.ruleMgr.execute(ruleName, null, "", p);
        }
        
        /// <summary>
        /// 
        /// </summary>
        public override void completeDataRetrieval()
        {
            if (m_rules != null)
            {
                m_rules.waitForCompletion(true);
                IBizObjList mediaCacheList = m_rules.CallResults as IBizObjList;
                useData(mediaCacheList);
                setupEditPriv();
                updateActionState();
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediaCacheList"></param>
        public virtual void useData(IBizObjList mediaCacheList)
        {
            m_mediaCacheList = mediaCacheList;
            if (m_listDisplay != null)
                m_listDisplay.showMediaCacheList(mediaCacheList.createCursor());
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediaCache"></param>
        public virtual void useData(IBizObj mediaCache)
        {
            m_currentMediaCache = mediaCache;
            if (mediaCache != null)
                foreach (IMediaCacheDetailsDisplay detailsDisplay in m_detailsDisplay)
                    detailsDisplay.showMediaCache(mediaCache);
        }
        
        /// <summary>
        /// 
        /// </summary>
        public IBizObj SelectedMediaCache
        {
            get
            {
				if (m_currentMediaCache == null && m_mediaCacheList != null && m_mediaCacheList.numElements > 0)
					m_currentMediaCache = m_mediaCacheList[0];
                return m_currentMediaCache;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        public Privileges HighestPrivilege
        {
            get
            {
                Privileges priv = Privileges.None;
                if(m_mediaCacheList != null)
                    foreach (IBizObj mediaCache in m_mediaCacheList)
                    {
                        Privileges cachePriv = CurrentPrincipal.getPrivilege("AppFunctions." + mediaCache.get("id"));
                        if (cachePriv > priv)
                            priv = cachePriv;
                    }
                return priv;
            }
        }
        
        protected virtual void setupEditPriv()
        {
            m_editPriv = m_currentMediaCache == null?Privileges.ReadOnly:
                this.CurrentPrincipal.getPrivilege("AppFunctions." + m_currentMediaCache.KeyValue);
        }
        
        protected void updateCache()
        {
            m_currentMediaCache.clearDirtyFlags();
            object oldId = m_currentMediaCache.get("id");
            if (m_currentMediaCache != null)
                foreach (IMediaCacheDetailsDisplay detailsDisplay in m_detailsDisplay)
                    detailsDisplay.updateMediaCache(m_currentMediaCache);
            IResults rc = App.ruleMgr.execute("updateCompanyImageCacheLibrary", new object[] { m_currentMediaCache, null });
            if (rc.ExceptionInfo == null)
            {
                m_currentMediaCache.put("id", rc.CallResults);
                for (int x = 0; x < m_mediaCacheList.numElements; ++x)
                    if (m_mediaCacheList[x].KeyValue == oldId)
                    {
                        IBizObj mci = m_mediaCacheList[x];
                        foreach (IBizObjField fld in mci.type.FieldList)
                            mci.put(fld.Name, m_currentMediaCache.get(fld.Name));
                        break;
                    }

                m_listDisplay.SelectedMediaCache = m_currentMediaCache;
                useData(m_mediaCacheList);
            }
            else
                OnBizProcException(this, "MediaCacheManagementBizProc.updateCache", rc.ExceptionInfo);
        }
        
        protected void addCache()
        {
            IBizObj company = null;
            IResults rc = App.ruleMgr.execute("createNewCompanyImageCacheLibrary", company);
            string newId = rc.CallResults as string;
            if (newId != null)
            {
                rc = App.ruleMgr.execute("getImageCacheDetails", newId);

                IBizObj imgCache = rc.CallResults as IBizObj;
                if (imgCache != null)
                {
                    IBizObj mci = m_mediaCacheList.insertNew();
                    foreach (IBizObjField fld in mci.type.FieldList)
                        mci.put(fld.Name, imgCache.get(fld.Name));
                }
                else
                    OnBizProcException(this, "addCache", rc.ExceptionInfo);
                m_listDisplay.SelectedMediaCache = imgCache;
                useData(m_mediaCacheList);
            }
            else
                OnBizProcException(this, "addCache", rc.ExceptionInfo);
        }
        
        protected void deleteCache()
        {
            IResults rc = App.ruleMgr.execute("");
        }
        
        protected void setEditMode()
        {
            m_inEditMode = (bool)App.sessionData["BpEditMode"];
            updateActionState();
        }

        /// <summary>
        /// 
        /// </summary>
        public override void updateActionState()
        {
            ((GenericAction)Actions["SwitchEdit"]).Enabled = m_editPriv > Privileges.ReadOnly;

            if (m_inEditMode)
            {
                ((GenericAction)Actions["AddCache"]).Enabled = m_editPriv >= Privileges.Add;
                ((GenericAction)Actions["UpdateCache"]).Enabled = m_editPriv >= Privileges.Edit;
                ((GenericAction)Actions["DeleteCache"]).Enabled = m_editPriv >= Privileges.Delete;
            }
            else
            {
                ((GenericAction)Actions["AddCache"]).Enabled = false;
                ((GenericAction)Actions["UpdateCache"]).Enabled = false;
                ((GenericAction)Actions["DeleteCache"]).Enabled = false;
            }

            if (m_listDisplay != null)
                m_listDisplay.EditPrivilege = m_inEditMode ? m_editPriv : Privileges.ReadOnly;
            foreach (IMediaCacheDetailsDisplay detailsDisplay in m_detailsDisplay)
                detailsDisplay.EditPrivilege = m_inEditMode ? m_editPriv : Privileges.ReadOnly;
        }
        
        protected void setupActions()
        {
			Actions.addAction(new GenericAction("SwitchEdit", false, setEditMode, this));
			Actions.addAction(new GenericAction("UpdateCache", false, updateCache, this));
			Actions.addAction(new GenericAction("AddCache", false, addCache, this));
			Actions.addAction(new GenericAction("DeleteCache", false, deleteCache, this));
        }
		*/
	}
}
